package 标准接口;



import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;


public class GetRequestinfo {

//第一步做一次就行了  记录下来
	/**
	 * 模拟缓存服务
	 */
	private static final Map<String,String> SYSTEM_CACHE = new HashMap <>();

	/**
	 * ecology 系统发放的授权许可证(appid)
	 */
	private static final String APPID = "EEAA5436-7577-4BE0-8C6C-89E9D44801EA";

	public static void test() {
		String  reqeustids="722840,726285";
		String  userids="19945,3904";

		reqeustids="886889,887098,895328,895315,895312,887459,887469";
		userids="16630,16630,16630,16630,16630,16630,16630,16630,16630,16630";
		reqeustids="942045";
		userids="243";

		String requestid[]=reqeustids.split(",");
		String userid[]=userids.split(",");


		for(int i=0;i<requestid.length;i++){
			HttpRequest.closeCookie();//每次调用要执行 清人员缓存的
			System.out.println(requestid[i]+"   "+userid[i]);
			String param="requestId="+requestid[i];
			testRestful("http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/getWorkflowRequest",param,userid[i]);

		}
	}
	public static void main(String[] args) {
		String  reqeustids="722840";
		String  userids="19945";


		reqeustids="886889,887098,5674";userids="16630,16630,243";

		reqeustids="1116421";
		userids="243";

		String requestid[]=reqeustids.split(",");
		String userid[]=userids.split(",");


		for(int i=0;i<requestid.length;i++){
			HttpRequest.closeCookie();//每次调用要执行 清人员缓存的
			System.out.println(requestid[i]+"   "+userid[i]);
			String param="requestId="+requestid[i];
			HttpRequest.closeCookie();

			testRestful("http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/getWorkflowRequest",param,userid[i]);

		}
	}
	/**
	 * 第一步：
	 *
	 * 调用 ecology 注册接口,根据 appid 进行注册,将返回服务端公钥和 Secret 信息
	 */

	public static Map<String,Object> testGetoken(String address){
		// 从系统缓存或者数据库中获取 ECOLOGY 系统公钥和 Secret 信息

		String secret ="6a393956-42c1-4b75-8b77-91c342ed90f1";
		String spk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmIqeh3HlAxMNSjepuY1YsRj6pqfX/r88v1xOKI7+vIXDpMLgmaA8ytRGTyHaxscXiXxcCKE6T+z54qrxaxw6b4/WSfcH8QaghHhQRAtvMWxP/SC1Vg5dniQMUVvrsB2wbf0DfyiftGeBf8gJbe1uWsXrMZLZE3olrDMCjylo8SwIDAQAB";

		SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",spk);
		//ECOLOGY 返回的系统密钥
		SYSTEM_CACHE.put("SERVER_SECRET",secret);
		RSA rsa = new RSA(null,spk);
		//对秘钥进行加密传输，防止篡改数据
		String encryptSecret = rsa.encryptBase64(secret,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
		//调用 ECOLOGY 系统接口进行注册
		String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
				.header("appid",APPID)
				.header("secret",encryptSecret)
				.header("time","3600")
				.execute().body();
		//System.out.println("testGetoken()："+data);
		Map<String,Object> datas = JSONUtil.parseObj(data);
		//ECOLOGY 返回的 token
		// TODO 为 Token 缓存设置过期时间
		SYSTEM_CACHE.put("SERVER_TOKEN",StrUtil.nullToEmpty((String)datas.get("token")));
		return datas;
	}




	public static String testRestful(String address,String api,String jsonParams,String userid){
		//ECOLOGY 返回的 token
		String  token = (String) testGetoken(address).get("token");
		String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");


		//System.out.println(spk);

		//封装请求头参数
		RSA rsa = new RSA(null,spk);
		//对用户信息进行加密传输,暂仅支持传输 OA 用户 ID
		String encryptUserid = rsa.encryptBase64(userid,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
		//调用 ECOLOGY 系统接口
		String data = HttpRequest.get(address + api)
				.header("appid",APPID)
				.header("token",token)
				.header("userid",encryptUserid)
				.body(jsonParams)
				.execute().body();


		System.out.println("token:"+token);
		System.out.println("userid:"+encryptUserid);

		System.out.println("testRestful()："+data);


		return data;
	}



}
